/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.controller;

import com.alibaba.fastjson.JSON;
import com.solid4j.auth.bean.MenuZtree;
import com.solid4j.auth.common.Constants;
import com.solid4j.auth.model.Menu;
import com.solid4j.auth.model.Privilege;
import com.solid4j.auth.page.Page;
import com.solid4j.auth.service.MenuService;
import com.solid4j.auth.service.PrivilegeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: chengfeng
 * @date: 2017/5/4
 */
@Controller
@RequestMapping("/auth/privilege")
public class PrivilegeController {

    @Resource
    private PrivilegeService privilegeService;

    @Resource
    private MenuService menuService;

    @RequestMapping("/list")
    public String list(Privilege privilege, @RequestParam(defaultValue = "1", required = true) Integer currentPage,
                       Model model) {
        String name = privilege.getPrivilegeName();
        StringBuilder condition = new StringBuilder("");
        if(!StringUtils.isEmpty(name)) {
            condition.append("privilege_name like '%" + name.trim() + "%'");
        }
        Page<Privilege> page = privilegeService.getPrivilegeList(condition.toString(), currentPage, Constants.PAGE_SIZE, "");
        model.addAttribute("page", page);
        return "/privilege/list";
    }

    @RequestMapping("/redirect")
    public String redirect(@RequestParam(defaultValue = "save", required = true) String type, @RequestParam(value = "id", required = false) Integer id, Model model) {
        if ("save".equals(type)) {
            List<MenuZtree> menuZtrees = getMenuTree();
            String treeJson = JSON.toJSONString(menuZtrees);
            model.addAttribute("treeJson", treeJson);
            return "/privilege/save";
        } else if ("update".equals(type)) {
            Privilege privilege = privilegeService.get(id);
            Set<Menu> menus = privilege.getMenus();
            if (menus.isEmpty()){
                return null;
            }

            List<MenuZtree> menuZtrees = getMenuTree();
            for (Menu menu : menus){
                for (MenuZtree tree:menuZtrees){
                    if (tree.getId()==menu.getId()){
                        tree.setChecked(true);
                    }
                }
            }

            String treeJson = JSON.toJSONString(menuZtrees);
            model.addAttribute("privilege", privilege);
            model.addAttribute("treeJson", treeJson);
            return "/privilege/update";
        }
        return null;
    }

    @RequestMapping("/save")
    public String save(@ModelAttribute Privilege privilege, @RequestParam(defaultValue = "", value = "") String menuIds) {
        Set<Menu> menus = new LinkedHashSet<Menu>();
        if (!"".equals(menuIds)) {
            String[] menuIdArr = menuIds.split(",");
            for (String menuId : menuIdArr) {
                menus.add(menuService.get(Integer.parseInt(menuId)));
            }
        }
        privilege.setMenus(menus);
        privilegeService.save(privilege);
        return "redirect:/auth/privilege/list.do?currentPage=1";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute Privilege privilege, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "", defaultValue = "") String menuIds) {
        Privilege newPrivilege = privilegeService.get(privilege.getId());
        Set<Menu> menus = new LinkedHashSet<Menu>();
        newPrivilege.setPrivilegeName(privilege.getPrivilegeName());
        if (!"".equals(menuIds)) {
            String[] menuIdArr = menuIds.split(",");
            for (String menuId : menuIdArr) {
                menus.add(menuService.get(Integer.parseInt(menuId)));
            }
        }
        newPrivilege.setMenus(menus);
        privilegeService.update(newPrivilege);
        return "redirect:/auth/privilege/list.do?currentPage=" + currentPage;
    }

    @RequestMapping(value = "/delete")
    public String delete(@RequestParam(value = "id") Integer id, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        Privilege privilege = privilegeService.get(id);
        privilegeService.delete(privilege);
        return "redirect:/auth/privilege/list.do?currentPage=" + currentPage;
    }

    /**
     * 将菜单处理为menuZtree
     * @return
     */
    private List<MenuZtree> getMenuTree(){
        List<Menu> menuList = menuService.getMenus();
        List<MenuZtree> menuZtrees = new LinkedList<MenuZtree>();
        if (!menuList.isEmpty()) {
            for (Menu menu : menuList) {
                MenuZtree menuZtree = new MenuZtree();
                menuZtree.setId(menu.getId());
                menuZtree.setName(menu.getMenuName());
                menuZtree.setpId(menu.getParentId());
                menuZtree.setOpen(true);
                menuZtrees.add(menuZtree);
            }
        }
        return menuZtrees;
    }

}
